﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace ConsoleSeoApp
{
    public class LinkTool
    {
        public static string DoBackLinkChecker(string domainName)
        {
            string result = "";
            HtmlNodeCollection htmlNodes = ExtractResult(domainName, Constants.BACKLINKCHECKER);
            if (htmlNodes != null && htmlNodes.Count > 0)
            {
                result = htmlNodes[0].OuterHtml;
            }

            return result;
        }

        public static string DoLinkAnalyser(string domainName)
        {
            string result = "";
            HtmlNodeCollection htmlNodes = ExtractResult(domainName, Constants.LINKANALYSER);
            if (htmlNodes != null && htmlNodes.Count > 0)
            {
                result = htmlNodes[0].OuterHtml;
            }

            return result;
        }

        //public static string DoLinkExtractor(string domainName)
        //{
        //    string result = "";
        //    HtmlNodeCollection htmlNodes = ExtractResult(domainName, Constants.LINKEXTRACTOR);
        //    if (htmlNodes != null && htmlNodes.Count > 0)
        //    {
        //        result = htmlNodes[0].InnerHtml;
        //    }

        //    return result;
        //}

        //public static string DoLinkPopularityChecker(string domainName)
        //{
        //    string result = "";
        //    HtmlNodeCollection htmlNodes = ExtractResult(domainName, Constants.LINKPOPULARITYCHECKER);
        //    if (htmlNodes != null && htmlNodes.Count > 0)
        //    {
        //        result = htmlNodes[0].OuterHtml;
        //    }

        //    return result;
        //}

        public static string DoLinkSuggestionGenerator(string keyword, string searchType, string searchText)
        {
            string result = "";
            string url = string.Format("{0}{1}/", Constants.BASEURL, Constants.LINKSUGGESTIONGENERATOR);
            string postData = string.Format("f_keyword={0}&f_type={1}&f_search={2}&a=submit", keyword.Replace(" ", "+"), searchType, searchText);//f_keyword=money+making&f_type=quotes&f_search=13&a=submit
            string response = WebHelper.PostData(url, postData);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(response);
            HtmlNodeCollection htmlNodes = doc.DocumentNode.SelectNodes("//div[@class='sl-tool-results']");
            if (htmlNodes != null && htmlNodes.Count > 0)
            {
                result = htmlNodes[0].OuterHtml;
            }

            return result;
        }

        public static string DoMultipleBacklinkChecker(string domainName)
        {
            string result = "";
            string postData = string.Format("f_urls={0}&a=submit", domainName);//f_urls=alexa.com%0D%0Agoogle.com&a=submit
            HtmlNodeCollection htmlNodes = ExtractResult(domainName, Constants.MULTIPLEBACKLINKCHECKER, postData);
            if (htmlNodes != null && htmlNodes.Count > 0)
            {
                result = htmlNodes[0].OuterHtml;
            }

            return result;
        }

        public static string DoNoFollowFinder(string domainName)
        {
            string result = "";
            HtmlNodeCollection htmlNodes = ExtractResult(domainName, Constants.NOFOLLOWFINDER);
            if (htmlNodes != null && htmlNodes.Count > 0)
            {
                result = htmlNodes[0].OuterHtml;
            }

            return result;
        }

        public static string DoReciprocalLinkChecker(string domainName, string statUrl)
        {
            string result = "";
            string postData = string.Format("f_url={0}&stat_url={1}&a=submit", domainName, statUrl); //f_url=referredlink.com&stat_url=alexa.com%0D%0Agoogle.com&a=submit
            HtmlNodeCollection htmlNodes = ExtractResult(domainName, Constants.RECIPROCALLINKCHECKER, postData);
            if (htmlNodes != null && htmlNodes.Count > 0)
            {
                result = htmlNodes[0].OuterHtml;
            }

            return result;
        }

        private static HtmlNodeCollection ExtractResult(string domainName, string toolName)
        {
            string url = string.Format("{0}{1}/", Constants.BASEURL, toolName);
            string postData = string.Format(Constants.POSTDATACOMMON, domainName);
            string response = WebHelper.PostData(url, postData);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(response);
            HtmlNodeCollection result = doc.DocumentNode.SelectNodes("//div[@class='sl-tool-results']");
            return result;
        }

        private static HtmlNodeCollection ExtractResult(string domainName, string toolName, string postData)
        {
            string url = string.Format("{0}{1}/", Constants.BASEURL, toolName);
            string response = WebHelper.PostData(url, postData);
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(response);
            HtmlNodeCollection result = doc.DocumentNode.SelectNodes("//div[@class='sl-tool-results']");
            return result;
        }

    }
}
